<?php

namespace frontend\modules\Apply\controllers;

use common\models\c2\entity\ActivityModel;
use common\models\c2\entity\ActivityPlayerModel;
use common\models\c2\statics\ActivityPlayerType;
use common\models\c2\statics\Whether;
use frontend\components\behaviors\WechatAuthBehavior;
use frontend\controllers\ActivityController;
use frontend\models\ActivityApplyForm;
use Yii;
use yii\web\Controller;
use yii\web\NotFoundHttpException;

/**
 * Default controller for the `apply` module
 */
class DefaultController extends ActivityController
{

    // public $layout = '/main-empty';
    /**
     * {@inheritdoc}
     */
    public function behaviors()
    {
        return [
            'wechatFilter' => [
                'class' => WechatAuthBehavior::className()
            ]
        ];
    }

    /**
     * Renders the index view for the module
     * @param null $c
     * @return string
     * @throws NotFoundHttpException
     */
    public function actionIndex($c = null)
    {
        $activity = ActivityModel::findOne(['seo_code' => $c]);
        if (is_null($activity) || $activity->is_released == Whether::TYPE_NO) {
            throw new NotFoundHttpException(Yii::t('app.c2', 'Activity disable.'));
        }
        $this->activity = $activity;
        $model = new ActivityPlayerModel();
        $model->loadDefaultValues();
        // $model->user_id = Yii::$app->user->id;
        $model->user_id = 1;
        $model->activity_id = $activity->id;
        $model->type = ActivityPlayerType::TYPE_USER_APPLY;

        if ($model->load(Yii::$app->request->post())) {
            if ($model->save()) {
                Yii::$app->session->setFlash($model->getMessageName(), [Yii::t('app.c2', 'Apply successful.')]);
            } else {
                Yii::$app->session->setFlash($model->getMessageName(), $model->errors);
            }
        }

        return (Yii::$app->request->isAjax) ? $this->renderAjax('index', [
            'activityModel' => $activity, 'model' => $model,]) : $this->render('index', ['activityModel' => $activity, 'model' => $model,]);
    }
}
